package com.custom.adz.custom;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

/**
 * Created by Administrator on 2017/6/7.
 * 自定义折线图
 */

public class LineChartView extends View {
    /*折线的颜色*/
    private int mLineColor2;
    /*绘制表头文字画笔*/
    private Paint mPaintText = new Paint();
    /*绘制表格的画笔*/
    private Paint mPaintLine = new Paint();
    /*折线的画笔*/
    private Paint mPaintLine2 =new Paint();
    /*坐标点的小圆点画笔*/
    private Paint mPointPaint = new Paint();
    private float mSmallDotRadius = 4;
    private TypedValue typedValue;
    private int mPaintClolor;
    private final String [] ORDINATES={"23","22","21","20","19","18","17","16","15","0"};
    private final String [] X_COORDINATE={"4:00","5:00","6:00","7:00","8:00","9:00","10:00","11:00",};

    public LineChartView(Context context) {
        super(context);
        init();
    }

    public LineChartView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public LineChartView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mLineColor2 = Color.GREEN;
        typedValue=new TypedValue();
        mPaintClolor =getResources().getColor(R.color.blue_40);
    }


    /**
     * @param canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        /*获取控件总宽高*/
        float totalWidth = getWidth();
        float totalHeight = getHeight();
        /*左边取总宽度的八分之一绘制表格头部*/
        float textWide = totalWidth / 9;
        float linHeight=totalHeight/10;
        /*左边留一点空白*/
        float left_offset = 10;
        /*折线图的总宽度等于控件的总宽度减去表头和留白*/
        float chartWide = totalWidth - textWide - left_offset;
        float chartheight=totalHeight-linHeight-left_offset;
        /*一共十行，设置每一行的垂直坐标*/
        float[] mLineYs = new float[]{
                linHeight+left_offset+chartheight*0/10,
                linHeight+left_offset+chartheight*1/10,
                linHeight+left_offset+chartheight*2/10,
                linHeight+left_offset+chartheight*3/10,
                linHeight+left_offset+chartheight*4/10,
                linHeight+left_offset+chartheight*5/10,
                linHeight+left_offset+chartheight*6/10,
                linHeight+left_offset+chartheight*7/10,
                linHeight+left_offset+chartheight*8/10,
                linHeight+left_offset+chartheight*9/10};
        /*一共八列，设置每一列的水平坐标*/
        float[] mLineXs = new float[]{
                textWide + left_offset + chartWide * 0 / 9,
                textWide + left_offset + chartWide * 1 / 9,
                textWide + left_offset + chartWide * 2 / 9,
                textWide + left_offset + chartWide * 3 / 9,
                textWide + left_offset + chartWide * 4 / 9,
                textWide + left_offset + chartWide * 5 / 9,
                textWide + left_offset + chartWide * 6 / 9,
                textWide + left_offset + chartWide * 7 / 9,
                textWide + left_offset + chartWide * 8 /9 ,
        };

        float [] line={
                linHeight+left_offset+chartheight*6/10,
                linHeight+left_offset+chartheight*5/10,
                linHeight+left_offset+chartheight*5/10,
                linHeight+left_offset+chartheight*5/10,
                linHeight+chartheight*5/10-left_offset,
                linHeight+left_offset+chartheight*4/10,
                linHeight+left_offset+chartheight*3/10,
                linHeight+left_offset+chartheight*3/10
        };

        /*绘制表头文字*/
        mPaintText.setStyle(Paint.Style.FILL);
        mPaintText.setColor(mPaintClolor);
        mPaintText.setAlpha(226);
        mPaintText.setTextSize(28);
        /*从中间开始绘制*/
        mPaintText.setTextAlign(Paint.Align.CENTER);
        /*测量文字大小，并计算偏移量*/
        Paint.FontMetrics fontMetrics = mPaintText.getFontMetrics();
        float textBaseLineOffset = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;

        /*绘制表格画笔设置*/
        mPaintLine.setStyle(Paint.Style.STROKE);
        mPaintLine.setAntiAlias(true);
        mPaintLine.setColor(mPaintClolor);
        mPaintLine.setAlpha(80);
        mPaintLine.setStrokeWidth(1);
        /*开始绘制表格*/
        /*绘制横线*/
        for(int i=0;i<10;i++){
            canvas.drawLine(mLineXs[0], mLineYs[i], totalWidth, mLineYs[i], mPaintLine);
            canvas.drawText(ORDINATES[i], (float)(textWide*0.8), mLineYs[i] + textBaseLineOffset, mPaintText);
        }
        /*绘制竖线*/
        for(int i=0;i<9;i++){
            canvas.drawLine(mLineXs[i], mLineYs[0], mLineXs[i], mLineYs[9], mPaintLine);
            if (i!=0){
                canvas.drawText(X_COORDINATE[i-1],mLineXs[i], totalHeight, mPaintText);
            }

        }
        mPaintLine2.setStyle(Paint.Style.STROKE);
        /*设置透明度，取值范围为0~255，数值越小越透明，0表示完全透明*/
        mPaintLine2.setAlpha(0);
        mPaintLine2.setAntiAlias(true);
        mPaintLine2.setColor(mLineColor2);
        mPaintLine2.setStrokeWidth(5);
        if (typedValue.resourceId==R.color.white){
            /*PorterDuff.Mode.SCREEN 上下层都显示。*/
            mPaintLine2.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SCREEN));
        }else {
            /*PorterDuff.Mode.DARKEN 上下层都显示。变暗*/
            mPaintLine2.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DARKEN));
        }
        /*画节点处的小圆点的画笔设置*/
        mPointPaint.setStyle(Paint.Style.STROKE);
        mPointPaint.setAntiAlias(true);
        mPointPaint.setColor(mPaintClolor);
        /*绘制第二条折线*/
        mPaintLine.setColor(mLineColor2);
        for (int i = 0; i <7; i++) {
            /*折线图的折线的画笔设置粗一点*/
            mPaintLine.setStrokeWidth(5);
            /*计算当前节点的坐标值*/
            float prePointX =mLineXs[i+1];
            float prePointY =line[i];
            /*计算下一个节点的坐标值*/
            float nextPointX=mLineXs[i + 2];
            float nextPointY=line[i+1];
            /*连接当前坐标和下一个坐标，绘制线段*/
            canvas.drawLine(prePointX, prePointY, nextPointX, nextPointY, mPaintLine2);
            /*当前节点坐标处绘制小圆*/
            canvas.drawCircle(prePointX, prePointY, mSmallDotRadius, mPointPaint);
        }
         /*折线图的最后一个节点的坐标*/
        float lastPointX=mLineXs[8];
        float lastPointY= line[7];
        /*绘制最后一个节点的外围小圆*/
        canvas.drawCircle(lastPointX,lastPointY ,mSmallDotRadius, mPointPaint);
    }
}
